package com.wise.gemmes.syncInfo;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.dbcp.BasicDataSource;
import org.jeecgframework.core.util.DynamicDBUtil;
import org.jeecgframework.web.system.pojo.base.TSBaseUser;
import org.jeecgframework.web.system.pojo.base.TSDepart;
import org.jeecgframework.web.system.pojo.base.TSUser;
import org.jeecgframework.web.system.pojo.base.TSUserOrg;
import org.jeecgframework.web.system.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Scope("prototype")
@Controller
@RequestMapping("/syncInfoController")
public class SyncInfoController {
	
	@Autowired
	private SystemService systemService;

	public void sync() {

		String driverClassName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
		//172.16.1.171
		String url = "jdbc:sqlserver://192.168.6.30:1433;DatabaseName=ecology";
		String dbUser = "chaxun";
		String dbPassword = "123";

		BasicDataSource dataSource = new BasicDataSource();
		dataSource.setDriverClassName(driverClassName);
		dataSource.setUrl(url);
		dataSource.setUsername(dbUser);
		dataSource.setPassword(dbPassword);

		JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
		
		/*TSDepart t = new TSDepart();
		t.setId("a1");
		t.setDepartname("光洋");
		systemService.save(t);
		
		TSDepart t7 = new TSDepart();
		t7.setId("a5");
		t7.setDepartname("液压");
		systemService.save(t7);
		
		TSDepart t3 = new TSDepart();
		t3.setId("a7");
		t3.setDepartname("科德");
		systemService.save(t3);
		
		TSDepart t4 = new TSDepart();
		t4.setId("a9");
		t4.setDepartname("数控大厦");
		systemService.save(t4);
		
		TSDepart t5 = new TSDepart();
		t5.setId("a12");
		t5.setDepartname("WISE");
		systemService.save(t5);
		
		TSDepart t6 = new TSDepart();
		t6.setId("a22");
		t6.setDepartname("沈阳");
		systemService.save(t6);


		//设置部门
		String sql = "select h.id,h.departmentname,h.departmentmark,h.subcompanyid1,h.supdepid from HrmDepartment h where canceled is null";
		List<Map<String, Object>> delist = DynamicDBUtil.findList(jdbcTemplate, sql);
		for (Map<String, Object> map : delist) {
			TSDepart tSDepart = new TSDepart();
			tSDepart.setId(map.get("id").toString());
			tSDepart.setDepartname((String) map.get("departmentname"));
			systemService.save(tSDepart);
		}
		
		List<Map<String, Object>> delist2 = DynamicDBUtil.findList(jdbcTemplate, sql);
		for (Map<String, Object> map : delist2) {
			TSDepart tSDepart = systemService.getEntity(TSDepart.class, map.get("id").toString());
			TSDepart t2;
			if((Integer)map.get("supdepid")==0) {
				t2 = systemService.getEntity(TSDepart.class, "a" + map.get("subcompanyid1").toString());
			}else {
				t2 = systemService.getEntity(TSDepart.class, map.get("supdepid").toString());
			}
			tSDepart.setTSPDepart(t2);

			systemService.saveOrUpdate(tSDepart);
		}*/

		String sql = "select h.id,h.lastname,h.email,h.mobile,h.telephone,h.messagerurl,h.createrid,h.createdate,h.departmentid,h.status from HrmResource h where status not in(5,7)";
		List<Map<String, Object>> relist = DynamicDBUtil.findList(jdbcTemplate, sql);
		System.out.println("------" + relist.size());
		for (Map<String, Object> map : relist) {
			//用户表
			/*TSUser tSUser = new TSUser();
			tSUser.setId(map.get("id").toString());
			tSUser.setEmail((map.get("email"))==null?"":(map.get("email").toString()));
			tSUser.setMobilePhone((map.get("mobile"))==null?"":(map.get("mobile").toString()));
			tSUser.setOfficePhone((map.get("telephone"))==null?"":(map.get("telephone").toString()));
			tSUser.setCreateName(map.get("createrid").toString());
			try {
				tSUser.setCreateDate(getDate(map.get("createdate").toString()));
			} catch (ParseException e) {
				e.printStackTrace();
			}
			tSUser.setPassword("000000");
			tSUser.setRealName(map.get("lastname").toString());
			//tSUser.setStatus(Short.parseShort(map.get("status").toString()));
			tSUser.setStatus((short) 0);
			tSUser.setUserName(map.get("lastname").toString());
			tSUser.setDepartid(map.get("departmentid").toString());
			TSUser user = systemService.getEntity(TSUser.class, map.get("id").toString());
			if(user==null) {
				systemService.save(tSUser);
			}*/
			/*//用户表
			TSBaseUser tSBaseUser = new TSBaseUser();
			tSBaseUser.setId(map.get("id").toString());
			tSBaseUser.setPassword("000000");
			tSBaseUser.setRealName(map.get("lastname").toString());
			tSBaseUser.setStatus((short) 0);
			tSBaseUser.setUserName(map.get("lastname").toString());
			tSBaseUser.setDepartid(map.get("departmentid").toString());
			systemService.save(tSBaseUser);*/

			//用户部门关系表
			/*TSUserOrg tSUserOrg = new TSUserOrg();
			TSDepart tSDepart = systemService.getEntity(TSDepart.class, map.get("departmentid").toString());
			TSUser tSUser = systemService.getEntity(TSUser.class, map.get("id").toString());
			tSUserOrg.setTsDepart(tSDepart);
			tSUserOrg.setTsUser(tSUser);
			systemService.save(tSUserOrg);*/
		}
	}
	
	public Date getDate(String date) throws ParseException {
		java.text.SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd");
		Date d =  formatter.parse(date);
		return d;
	}
}
